package com.uobgroup.ews.uam.core.dao;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.Reference;
import com.uobgroup.ews.uam.core.domain.UAMUser;

@Repository
public class UAMUserDaoImpl extends AbstractJpaDao<UAMUser> implements UAMUserDao {

   public UAMUserDaoImpl() {
      setClazz(UAMUser.class);
   }

   @Override
   public long count() {
      return entityManager.createQuery("SELECT COUNT(o) FROM UAMUser o", Long.class).getSingleResult();
   }

   @Override
   public List<UAMUser> findAllWithoutDeletion() {
      return entityManager.createQuery("SELECT o FROM UAMUser o Where o.IsDeleted = false", UAMUser.class)
            .getResultList();
   }

   @Override
   public List<UAMUser> findAll(int firstResult, int maxResults) {
      return entityManager.createQuery("SELECT o FROM UAMUser o Where o.IsDeleted = false", UAMUser.class)
            .setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
   }

   @Override
   public UAMUser findUserbyUsername(String username) {
      List<UAMUser> list = entityManager.createQuery("SELECT o FROM UAMUser o WHERE o.username='" + username + "'",
            UAMUser.class).getResultList();
      return list.get(0);
   }

   @Override
   public boolean IsExist(String username) {
      List<UAMUser> list = entityManager.createQuery(
            "SELECT o FROM UAMUser o WHERE o.username='" + username.toLowerCase() + "'", UAMUser.class).getResultList();
      if (list.size() > 0)
         return true;
      else
         return false;

   }

   @SuppressWarnings("unchecked")
   public List<UAMUser> findUAMUserFilter(String username, Boolean IsAdmin, Boolean IsActive, Boolean IsDeleted,
         Boolean IsLocked, int firstResult, int maxResults) {
      String queryStr = "";
      Query query = null;
      if (IsAdmin.equals(new Boolean(false)) && IsActive.equals(new Boolean(false))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name ";
         query = entityManager.createQuery(queryStr);

      }
      else if (IsAdmin.equals(new Boolean(true)) && IsActive.equals(new Boolean(false))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Admin", IsAdmin);

      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(false))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Active", IsActive);
      }
      else if (IsLocked.equals(new Boolean(true)) && IsActive.equals(new Boolean(false))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Locked", IsLocked);
      }
      else if (IsAdmin.equals(new Boolean(true)) && IsActive.equals(new Boolean(true))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin and r.isActive=:Active";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Active", IsActive);
         query.setParameter("Admin", IsAdmin);
      }
      else if (IsAdmin.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsActive.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Admin", IsAdmin);
      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Active", IsActive);
      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsAdmin.equals(new Boolean(true))) {
         queryStr = "select r from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active and r.IsLocked=:Locked and r.IsAdmin=:Admin";
         query = entityManager.createQuery(queryStr);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Active", IsActive);
         query.setParameter("Admin", IsAdmin);
      }

      query.setParameter("name", "%" + username.toLowerCase() + "%");
      query.setFirstResult(firstResult).setMaxResults(maxResults);
      return query.getResultList();
   }
   
   @Override
   public long countUAMUserFilter(String username, Boolean IsAdmin, Boolean IsActive, Boolean IsDeleted,
         Boolean IsLocked) {
      String queryStr = "";
      TypedQuery<Long> query = null;
      if (IsAdmin.equals(new Boolean(false)) && IsActive.equals(new Boolean(false))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name ";
         query = entityManager.createQuery(queryStr, Long.class);

      }
      else if (IsAdmin.equals(new Boolean(true)) && IsActive.equals(new Boolean(false))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Admin", IsAdmin);

      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(false))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Active", IsActive);
      }
      else if (IsLocked.equals(new Boolean(true)) && IsActive.equals(new Boolean(false))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Locked", IsLocked);
      }
      else if (IsAdmin.equals(new Boolean(true)) && IsActive.equals(new Boolean(true))
            && IsLocked.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin and r.isActive=:Active";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Active", IsActive);
         query.setParameter("Admin", IsAdmin);
      }
      else if (IsAdmin.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsActive.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.IsAdmin=:Admin and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Admin", IsAdmin);
      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsAdmin.equals(new Boolean(false))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active and r.IsLocked=:Locked";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Active", IsActive);
      }
      else if (IsActive.equals(new Boolean(true)) && IsLocked.equals(new Boolean(true))
            && IsAdmin.equals(new Boolean(true))) {
         queryStr = "select COUNT(r) from UAMUser r where r.IsDeleted = false and lower(r.username) like :name "
               + " and r.isActive=:Active and r.IsLocked=:Locked and r.IsAdmin=:Admin";
         query = entityManager.createQuery(queryStr, Long.class);
         query.setParameter("Locked", IsLocked);
         query.setParameter("Active", IsActive);
         query.setParameter("Admin", IsAdmin);
      }

      query.setParameter("name", "%" + username.toLowerCase() + "%");
      return query.getSingleResult();
   }

   @Override
   public long countUserThatHasCountryReference(Reference country) {
      return entityManager.createQuery("SELECT COUNT(U) FROM UAMUser U WHERE U.country = :country", Long.class)
            .setParameter("country", country).getSingleResult();
   }

   /*
    * Phuong Tran
    */
   @Override
   public List<UAMUser> findUsersByName(String userName) {
      return entityManager.createQuery("SELECT o FROM UAMUser o WHERE o.username='" + userName + "'", UAMUser.class)
            .getResultList();
   }

}
